<?php
// +----------------------------------------------------------------------
// | Description: 产品
// +----------------------------------------------------------------------
// | Author: Michael_xu | gengxiaoxu@5kcrm.com 
// +----------------------------------------------------------------------

namespace app\crm\controller;

use app\admin\controller\ApiCommon;
use think\Hook;
use think\Request;

//合同产品绑定确认函
class ContractConfirmation extends ApiCommon
{
    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @other 其他根据系统设置
     **/
    public function _initialize()
    {
        $action = [
            'permission' => [''],
            'allow' => ['']
        ];
        Hook::listen('check_auth', $action);
        $request = Request::instance();
        $a = strtolower($request->action());
        if (!in_array($a, $action['permission'])) {
            parent::_initialize();
        }
    }

    /**
     * 确认函详情
     * @param
     * @return
     * @author Michael_xu
     */
    public function read()
    {
        $confirmationModel = model('ContractConfirmation');
        $param = $this->param;
        $data = $confirmationModel->getDataById($param["contract_id"],$param["product_id"]);
        if (!$data) {
            return resultArray(['error' => $confirmationModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }


    /**
     * 编辑确认函
     * @param
     * @return
     * @author Michael_xu
     */
    public function update()
    {
        $confirmationModel = model('ContractConfirmation');
        $param = $this->param;
        /*$userInfo = $this->userInfo;
        //数据详情
        $data = $confirmationModel->getDataById($param['contract_confirmation_id']);
        if (!$data) {
            return resultArray(['error' => $confirmationModel->getError()]);
        }*/
        //数据权限判断
        /*$userModel = new \app\admin\model\User();
        $auth_user_ids = $userModel->getUserByPer('crm', 'confirmation', 'update');
        //读写权限
        $rwPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'update');
        $wherePool = $confirmationModel->getWhereByPool();
        $resPool = db('crm_confirmation')->alias('confirmation')->where(['confirmation_id' => $param['id']])->where($wherePool)->find();
        if (!$resPool && !in_array($data['owner_user_id'],$auth_user_ids) && !$rwPre) {
            header('Content-Type:application/json; charset=utf-8');
            exit(json_encode(['code'=>102,'error'=>'无权操作']));
        }

        $param['user_id'] = $userInfo['id'];*/
        if ($confirmationModel->updateDataById($param)) {
            return resultArray(['data' => '编辑成功']);
        } else {
            return resultArray(['error' => $confirmationModel->getError()]);
        }
    }

}
